package org.theqcp.wow.sim.gui.tabs;

import java.awt.Component;
import java.awt.Container;
import java.util.HashMap;
import java.util.TreeMap;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout.ParallelGroup;
import javax.swing.GroupLayout.SequentialGroup;

import org.theqcp.wow.sim.gui.DataPoint;
import org.theqcp.wow.sim.gui.EnhSimGUIFrame;

public abstract class DataPanel extends JPanel {

    private static final long serialVersionUID = 2043030819058223878L;

    protected final HashMap<String, DataPoint> fieldNames = new HashMap<String, DataPoint>();

    /**
     * Creates a JPanel with a Label and Combo box.
     * 
     * @param esgFrame - Frame to receive data point container
     * @param name - Name of component (used for label and to identify data
     *        point)
     * @param list - Values for combo box
     */
    protected JPanel makeComboBox( EnhSimGUIFrame esgFrame, String name, String... list ) {
	JPanel t = new JPanel();
	JLabel l = new JLabel(name);
	JComboBox tf = new JComboBox(new DefaultComboBoxModel(list));
	t.setLayout(new BoxLayout(t, BoxLayout.X_AXIS));

	esgFrame.addDataPoint(fieldNames.get(name), tf);

	t.add(l);
	t.add(Box.createHorizontalStrut(5));
	t.add(Box.createHorizontalGlue());
	t.add(tf);

	return t;
    }

    /**
     * Creates a JPanel with a GroupLayout containing JLabels and JTextFields
     * for the provided field names
     * 
     * @param esgFrame - Frame to receive data point container
     * @param fieldNameArray - Field names to add
     * @return JPanel containing layout
     */
    protected JPanel makeTextFieldArray( EnhSimGUIFrame esgFrame, String... fieldNameArray ) {
	TreeMap<DataPoint, Component> labels = new TreeMap<DataPoint, Component>();
	TreeMap<DataPoint, Component> fields = new TreeMap<DataPoint, Component>();
	JPanel p = new JPanel();
	GroupLayout gl;

	for( String fieldName: fieldNameArray ) {
	    DataPoint dPoint = fieldNames.get(fieldName);
	    JLabel label = new JLabel(fieldName);
	    JTextField field = new JTextField(5);

	    label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));

	    labels.put(dPoint, label);
	    fields.put(dPoint, field);

	    esgFrame.addDataPoint(dPoint, field);
	}

	gl = new GroupLayout(p);

	SequentialGroup hGroup = gl.createSequentialGroup();
	SequentialGroup vGroup = gl.createSequentialGroup();
	ParallelGroup pg;

	pg = gl.createParallelGroup(Alignment.TRAILING);
	for( Component c: labels.values() ) {
	    pg.addComponent(c);
	}
	hGroup.addGroup(pg);

	pg = gl.createParallelGroup();
	for( Component c: fields.values() ) {
	    pg.addComponent(c);
	}
	hGroup.addGroup(pg);

	for( DataPoint dPoint: labels.keySet() ) {
	    pg = gl.createParallelGroup();
	    pg.addComponent(labels.get(dPoint)).addComponent(fields.get(dPoint));
	    vGroup.addGroup(pg);
	}

	gl.setHorizontalGroup(hGroup);
	gl.setVerticalGroup(vGroup);

	p.setLayout(gl);

	return p;
    }

    // @Deprecated
    protected void addCheckBox( EnhSimGUIFrame esgFrame, Container p, String name ) {
	JPanel t = new JPanel();
	JLabel l = new JLabel(name);
	JCheckBox cb = new JCheckBox();
	t.setLayout(new BoxLayout(t, BoxLayout.X_AXIS));

	esgFrame.addDataPoint(fieldNames.get(name), cb);

	t.add(l);
	t.add(Box.createHorizontalStrut(5));
	t.add(Box.createHorizontalGlue());
	t.add(cb);

	p.add(t);
    }

    // @Deprecated
    protected void addTextField( EnhSimGUIFrame esgFrame, Container p, String name ) {
	JPanel t = new JPanel();
	JLabel l = new JLabel(name);
	JTextField tf = new JTextField(5);
	t.setLayout(new BoxLayout(t, BoxLayout.X_AXIS));

	esgFrame.addDataPoint(fieldNames.get(name), tf);

	t.add(l);
	t.add(Box.createHorizontalStrut(5));
	t.add(Box.createHorizontalGlue());
	t.add(tf);

	p.add(t);
    }

    // @Deprecated
    protected void addTextFieldsInPanel( EnhSimGUIFrame esgFrame, Container p, String... list ) {
	for( String f: list ) {
	    JPanel t = new JPanel();
	    JLabel l = new JLabel(f);
	    JTextField tf = new JTextField(5);
	    t.setLayout(new BoxLayout(t, BoxLayout.X_AXIS));

	    esgFrame.addDataPoint(fieldNames.get(f), tf);

	    t.add(l);
	    t.add(Box.createHorizontalStrut(5));
	    t.add(Box.createHorizontalGlue());
	    t.add(tf);

	    p.add(t);
	}
    }

    // @Deprecated
    protected void addSpinnerBox( EnhSimGUIFrame esgFrame, Container p, String name, int max ) {
	JPanel t = new JPanel();
	JLabel l = new JLabel(name);
	JSpinner sp = new JSpinner(new SpinnerNumberModel(0, 0, max, 1));
	t.setLayout(new BoxLayout(t, BoxLayout.X_AXIS));

	esgFrame.addDataPoint(fieldNames.get(name), sp);

	t.add(l);
	t.add(Box.createHorizontalStrut(5));
	t.add(Box.createHorizontalGlue());
	t.add(sp);

	p.add(t);
    }

}
